package com.zp.self.module.工具.json转换.实战;

import com.zp.self.module.工具.json转换.jsonUtil.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：by peng.zeng
 * @date ：Created 2022/1/18 11:14
 * @description ：
 */
public class 合并json求最小值 {

    public static void main(String[] args) throws IOException {
        JSONArray mainJson = JsonUtil.getJson("C:\\Users\\peng.zeng\\Desktop\\fsdownload\\data.json");
        JSONArray otherJson_0 = JsonUtil.getJson("C:\\Users\\peng.zeng\\Desktop\\fsdownload\\data2.json");
        //1.将otherJson_0 记录<deviceId，JSONObject>的映射
        Map<String, JSONObject> deviceId_JSONObject_Map = new HashMap<>();
        int mainLength = mainJson.length();
        for (int i = 0; i < mainLength; i++) {
            JSONObject jsonObject = mainJson.getJSONObject(i);
            String deviceId = jsonObject.getString("deviceId");
            deviceId_JSONObject_Map.put(deviceId,jsonObject);
        }

        int otherJsonLength = otherJson_0.length();
        for (int i = 0; i < otherJsonLength; i++) {
            JSONObject json1 = otherJson_0.getJSONObject(i);
            String deviceId = json1.getString("deviceId");
            if(deviceId_JSONObject_Map.containsKey(deviceId)){
                JSONObject json0 = deviceId_JSONObject_Map.get(deviceId);
                int minH = Math.min(json0.getInt("minH"), json1.getInt("minH"));
                json0.put("minH",minH);
                int maxH = Math.max(json0.getInt("maxH"), json1.getInt("maxH"));
                json0.put("maxH",maxH);
            }else
                mainJson.put(json1);
        }

        JsonUtil.writeJson(mainJson,"C:\\Users\\peng.zeng\\Desktop\\hous_eu.json");

    }
}
